package com.x.erp.dao;

import com.x.erp.model.MultilevelActivityReward;
import com.x.framework.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 多级营销活动奖励记录信息DAO
 */
@Repository
public class MultilevelActivityRewardDao extends BaseDao {

	private final static String selectMultilevelActivityRewardListSql = "SELECT " +
			"emar.* " +
			"FROM " +
			"erp_multilevel_activity_reward emar " +
			"WHERE " +
			"1 = 1 ";
	/**
	 * 查询多级营销活动奖励记录列表
	 * @param multilevelActivityReward
	 * @return
	 * @throws Exception
	 */
	public List<MultilevelActivityReward> selectMultilevelActivityRewardList(MultilevelActivityReward multilevelActivityReward) throws Exception{
		StringBuilder sql = new StringBuilder(selectMultilevelActivityRewardListSql);
		List<Object> params = new ArrayList<>();
		searchCondition(sql, params, multilevelActivityReward);
		sql.append("ORDER BY emar.REWARD_LEVEL ASC ");
		return this.queryForList(sql.toString(), MultilevelActivityReward.class, params);
	}

	/**
	 * 查询条件
	 * @param sql
	 * @param params
	 * @param multilevelActivityReward
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, MultilevelActivityReward multilevelActivityReward) {
		if(multilevelActivityReward != null) {
			if(multilevelActivityReward.getRewardTotalLevel() != null){
				sql.append("AND emar.REWARD_TOTAL_LEVEL = ? ");
				params.add(multilevelActivityReward.getRewardTotalLevel());
			}
		}
	}
}
